using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.CompilerServices;
using TuyennvLibrary.SqlClient;
namespace TuyennvLibrary.ServerApi.Models
{
    public class Devices
    {
        public int id { get; set; }
        public string deviceid { get; set; }
        public string longitute { get; set; }
        public string latitude { get; set; }
        public string ip_address { get; set; }
        public string email { get; set; }
        public int platform { get; set; }
        public DateTime createddate { get; set; }


        public static string COLUMN_ID = "id";
        public static string COLUMN_DEVICEID = "deviceid";
        public static string COLUMN_LONGITUTE = "longitute";
        public static string COLUMN_LATITUDE = "latitude";
        public static string COLUMN_IP_ADDRESS = "ip_address";
        public static string COLUMN_EMAIL = "email";
        public static string COLUMN_PLATFORM = "platform";
        public static string COLUMN_CREATEDDATE = "createddate";

        public static string TableName = "Devices";

        public Devices()
        {
            this.id = 0;
            this.deviceid = string.Empty;
            this.longitute = string.Empty;
            this.latitude = string.Empty;
            this.ip_address = string.Empty;
            this.email = string.Empty;
            this.platform = 0;
            this.createddate = DateTime.Now;

        }
    }
    public class DevicesModel : BaseModel<Devices>
    {
        public DevicesModel()
        {
            table = Devices.TableName;
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Insert(Devices obj)
        {
            try
            {
                DB.Insert(table, new SqlParameter[] {
					new SqlParameter("deviceid",obj.deviceid),
					new SqlParameter("longitute",obj.longitute),
					new SqlParameter("latitude",obj.latitude),
					new SqlParameter("ip_address",obj.ip_address),
					new SqlParameter("email",obj.email),
					new SqlParameter("platform",obj.platform),
					new SqlParameter("createddate",obj.createddate),
                    
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Update(Devices obj)
        {
            try
            {
                DB.Update(table, new SqlParameter[] {
					new SqlParameter("deviceid",obj.deviceid),
					new SqlParameter("platform",obj.platform),
					new SqlParameter("createddate",obj.createddate),

                }, new SqlParameter[]{
					new SqlParameter("id",obj.id),
																																			 
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Delete(int id)
        {
            try
            {
                DB.Delete(table, new SqlParameter[] { 
					new SqlParameter("id",id),
																																			 
                });
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool DeleteMulti(string ListID)
        {
            try
            {
                if (ListID != string.Empty)
                {
                    foreach (string ID in ListID.Split(','))
                    {
                        Delete(int.Parse(ID));
                    }
                }
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<Devices> GetByPage(int page, int pagesize, List<Devices> source)
        {
            List<Devices> list = new List<Devices>();
            int index = (page - 1) * pagesize;
            int count = source.Count;
            if (pagesize > count)
                list = source;
            else if (pagesize > count - index)
                list = source.GetRange(index, count - index);
            else
                list = source.GetRange(index, pagesize);
            return list;
        }
        public List<Devices> GetListBy(DataTable dt)
        {
            List<Devices> list = new List<Devices>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Devices obj = new Devices();
                    obj.id = (dr["id"] is DBNull) ? 0 : int.Parse(dr["id"].ToString());
                    obj.deviceid = (dr["deviceid"] is DBNull) ? string.Empty : dr["deviceid"].ToString();
                    obj.longitute = (dr["longitute"] is DBNull) ? string.Empty : dr["longitute"].ToString();
                    obj.latitude = (dr["latitude"] is DBNull) ? string.Empty : dr["latitude"].ToString();
                    obj.ip_address = (dr["ip_address"] is DBNull) ? string.Empty : dr["ip_address"].ToString();
                    obj.email = (dr["email"] is DBNull) ? string.Empty : dr["email"].ToString();
                    obj.platform = (dr["platform"] is DBNull) ? 0 : int.Parse(dr["platform"].ToString());
                    obj.createddate = (dr["createddate"] is DBNull) ? DateTime.Now : DateTime.Parse(dr["createddate"].ToString());

                    list.Add(obj);
                }
            }
            return list;
        }
        public DataTable GetListAll()
        {
            try
            {
                DataTable dt = DB.SelectDataTable(table);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public Devices GetBy(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Devices obj = new Devices();
                    obj.id = (dr["id"] is DBNull) ? 0 : int.Parse(dr["id"].ToString());
                    obj.deviceid = (dr["deviceid"] is DBNull) ? string.Empty : dr["deviceid"].ToString();
                    obj.longitute = (dr["longitute"] is DBNull) ? string.Empty : dr["longitute"].ToString();
                    obj.latitude = (dr["latitude"] is DBNull) ? string.Empty : dr["latitude"].ToString();
                    obj.ip_address = (dr["ip_address"] is DBNull) ? string.Empty : dr["ip_address"].ToString();
                    obj.email = (dr["email"] is DBNull) ? string.Empty : dr["email"].ToString();
                    obj.platform = (dr["platform"] is DBNull) ? 0 : int.Parse(dr["platform"].ToString());
                    obj.createddate = (dr["createddate"] is DBNull) ? DateTime.Now : DateTime.Parse(dr["createddate"].ToString());

                    return obj;
                }
            }
            return new Devices();
        }
        public Devices GetByID(int id)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
					new SqlParameter("id",id),
																																			
                }, null);
                return GetBy(dt);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public Devices GetByDeviceid(string deviceid)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
					new SqlParameter("deviceid",deviceid),
																																			
                }, null);
                return GetBy(dt);
            }
            catch (Exception)
            {
            }
            return new Devices();
        }
    }
}
